import { defineStore } from "pinia";
import { setToken } from "../../utils/token";
import { queryUserInfo, userLogin } from "../../api";
import { showToast } from "vant";
const useUserStore = defineStore("user", {
  state: () => ({
    token: localStorage.getItem('token'),
    showLogin: false,
    userInfo:localStorage.getItem('userInfo')
  }),
  actions: {
    isShow(showLogin) {
      this.showLogin = showLogin;
    },
    login(data){
      userLogin(data).then(res=>{ 
        if(localStorage.getItem('forming')){
          showToast('登录成功,请继续提交')
        }else{
          showToast('登录成功')
        }
        this.token = res.token
        localStorage.setItem('token',res.token)
        this.showLogin = false;
        queryUserInfo().then(res=>{
          this.userInfo = res.data
          localStorage.setItem('userInfo',JSON.stringify(res.data))
        })
      })
    },
    loginOut(){
      this.token = '';
      this.showLogin = false;
      this.userInfo = {};
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
    },
    getUserInfo(){
      if(!this.token){
        return
      }
      queryUserInfo().then(res=>{
        this.userInfo = res.data
        localStorage.setItem('userInfo',JSON.stringify(res.data))
      })
    },
  },
});
export default useUserStore;
